<?php

/**
 * Interface for creating a new user
 * 
 * This page holds HTML forms for creating a new user.
 * @author James Ravenscroft
 * @license LGPL
 * @package Edify
 */

if(SESSION_AUTHENTICATED != true)
	exit;

$smarty->assign('formTitle', "Create New User");

if($_POST['submit'])
{
	$username = $_POST['username'];
	$password = $_POST['password'];
	$confpass = $_POST['confpass'];
	$email = $_POST['email'];
	$admin = $_POST['admin'];
	$errors = array();
	$error = false;
	if(strlen($username) < 3)
	{
		$error = true;
		$errors[] = "Please ensure that the username you have picked is longer than 2 characters.";
	}
	
	if(strlen($password) < 6)
	{
		$error = true;
		$errors[] = "The password must be longer than 5 characters.";
	}

	if($password != $confpass)
	{
		$error = true;
		$errors[] = "Please ensure that your passwords both match";
	}
	
	if($error == false)
	{
		$passcrypted = crypt($password, substr($password,0,2));
		$dbh->compile("INSERT INTO {$prefix}_users(username,password,email,admin)".
		" VALUES (:username ,:password, :email, :admin)");
		$dbh->execute(array('username' => $username, 'password' => $passcrypted,
		'email' => $email, 'admin' => $admin));
		print $dbh->getLastError();
		$pageContent = $smarty->fetch("admin/users/add_success.tpl");
	}
	else
	{
		$smarty->assign('messg', "There are some errors with your form. Please try again after".
		" changing the following");
		$smarty->assign('errors',$errors);
		$smarty->assign('username_value', $username);
		$smarty->assign('password_value', $password);
		$smarty->assign('confpass_value', $confpass);
		$smarty->assign('email_value', $email);
		$smarty->assign('admin',$admin);
		$pageContent = $smarty->fetch("admin/users/form.tpl");
	}
	
}
else
{
	//this will allow multiple languages in future incarnations
	$smarty->assign('admin', 0);
	$pageContent = $smarty->fetch("admin/users/form.tpl");
}
?>